// import _defineProperty from '@babel/runtime/helpers/defineProperty';
import macro from '@kitware/vtk.js/macro';
// import vtkMatrixBuilder from '@kitware/vtk.js/Common/Core/MatrixBuilder';
// import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray';
import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper';
// import vtkArrowSource from '@kitware/vtk.js/Filters/Sources/ArrowSource';
// import vtkAppendPolyData from '@kitware/vtk.js/Filters/General/AppendPolyData';
import vtkCubeSource from '@kitware/vtk.js/Filters/Sources/CubeSource';
import vtkTexture from '@kitware/vtk.js/Rendering/Core/Texture.js';
import ImageHelper from '@kitware/vtk.js/Common/Core/ImageHelper.js';
import AnnotatedCubePresets from '@kitware/vtk.js/Rendering/Core/AnnotatedCubeActor/Presets.js';
//VTK.js Orientation Indicator
function ownKeys(object, enumerableOnly) {
  const keys = Object.keys(object);
  if (Object.getOwnPropertySymbols) {
    let symbols: any[] = Object.getOwnPropertySymbols(object);
    enumerableOnly &&
      (symbols = symbols.filter(function (sym) {
        return Object!.getOwnPropertyDescriptor(object, sym)!.enumerable;
      })),
      keys.push(...keys, ...symbols);
  }
  return keys;
}

function _objectSpread(target, ...args) {
  for (let i = 1; i < arguments.length; i++) {
    const source = null != args[i] ? args[i] : {};
    i % 2
      ? ownKeys(Object(source), !0).forEach(function (key) {
          Object.defineProperty(target, key, source[key]);
        })
      : Object.getOwnPropertyDescriptors
      ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source))
      : ownKeys(Object(source), undefined).forEach(function (key) {
          //@ts-ignore
          Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
  }
  return target;
}
const FACE_TO_INDEX = {
  xPlus: 0,
  xMinus: 1,
  yPlus: 2,
  yMinus: 3,
  zPlus: 4,
  zMinus: 5,
}; // ----------------------------------------------------------------------------
// vtkAnnotatedCubeActor
// ----------------------------------------------------------------------------

function vtkAnnotatedCubeActor(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkAnnotatedCubeActor'); // Make sure face properties are not references to the default value

  model.xPlusFaceProperty = _objectSpread(model.xPlusFaceProperty);
  model.xMinusFaceProperty = _objectSpread(model.xMinusFaceProperty);
  model.yPlusFaceProperty = _objectSpread(model.yPlusFaceProperty);
  model.yMinusFaceProperty = _objectSpread(model.yMinusFaceProperty);
  model.zPlusFaceProperty = _objectSpread(model.zPlusFaceProperty);
  model.zMinusFaceProperty = _objectSpread(model.zMinusFaceProperty); // private variables

  let cubeSource = null;
  const canvas = document.createElement('canvas');
  const mapper = vtkMapper.newInstance();
  const texture = vtkTexture.newInstance();
  texture.setInterpolate(true); // private methods

  function updateFaceTexture(faceName, ...args) {
    const newProp = arguments.length > 1 && args[1] !== undefined ? args[1] : null;

    if (newProp) {
      // Object.assign(model[''.concat(faceName, 'FaceProperty')], newProp);
    }

    const prop = _objectSpread(
      _objectSpread(model.defaultStyle),
      //@ts-ignore
      model[''.concat(faceName, 'FaceProperty')],
    ); // set canvas resolution

    canvas.width = prop.resolution;
    canvas.height = prop.resolution;
    const ctxt = canvas.getContext('2d'); // set background color

    ctxt!.fillStyle = prop.faceColor;
    ctxt!.fillRect(0, 0, canvas.width, canvas.height); // draw edge

    if (prop.edgeThickness > 0) {
      ctxt!.strokeStyle = prop.edgeColor;
      ctxt!.lineWidth = prop.edgeThickness * canvas.width;
      ctxt!.strokeRect(0, 0, canvas.width, canvas.height);
    } // set face rotation

    ctxt!.save(); // vertical flip

    ctxt!.translate(0, canvas.height);
    ctxt!.scale(1, -1);
    ctxt!.translate(canvas.width / 2, canvas.height / 2);
    ctxt!.rotate(-Math.PI * (prop.faceRotation / 180.0)); // set foreground text

    const textSize = prop.fontSizeScale(prop.resolution);
    ctxt!.fillStyle = prop.fontColor;
    ctxt!.textAlign = 'center';
    ctxt!.textBaseline = 'middle';
    ctxt!.font = ''
      .concat(prop.fontStyle, ' ')
      .concat(textSize, 'px "')
      .concat(prop.fontFamily, '"');
    ctxt!.fillText(prop.text, 0, 0);
    ctxt!.restore();
    const vtkImage = ImageHelper.canvasToImageData(canvas);
    //@ts-ignore
    texture.setInputData(vtkImage, FACE_TO_INDEX[faceName]);
    publicAPI.modified();
  }

  function updateAllFaceTextures() {
    //@ts-ignore
    cubeSource = vtkCubeSource.newInstance({
      generate3DTextureCoordinates: true,
    });
    //@ts-ignore
    mapper.setInputConnection(cubeSource.getOutputPort());
    updateFaceTexture('xPlus', null);
    updateFaceTexture('xMinus', null);
    updateFaceTexture('yPlus', null);
    updateFaceTexture('yMinus', null);
    updateFaceTexture('zPlus', null);
    updateFaceTexture('zMinus', null);
  } // public methods

  publicAPI.setDefaultStyle = function () {
    model.defaultStyle = _objectSpread(_objectSpread(model.defaultStyle));
    updateAllFaceTextures();
  };

  publicAPI.setXPlusFaceProperty = function (prop) {
    return updateFaceTexture('xPlus', prop);
  };

  publicAPI.setXMinusFaceProperty = function (prop) {
    return updateFaceTexture('xMinus', prop);
  };

  publicAPI.setYPlusFaceProperty = function (prop) {
    return updateFaceTexture('yPlus', prop);
  };

  publicAPI.setYMinusFaceProperty = function (prop) {
    return updateFaceTexture('yMinus', prop);
  };

  publicAPI.setZPlusFaceProperty = function (prop) {
    return updateFaceTexture('zPlus', prop);
  };

  publicAPI.setZMinusFaceProperty = function (prop) {
    return updateFaceTexture('zMinus', prop);
  }; // constructor

  updateAllFaceTextures(); // set mapper
  //@ts-ignore
  mapper.setInputConnection(cubeSource.getOutputPort());
  // publicAPI.setMapper(mapper); // set texture

  publicAPI.addTexture(texture);
} // ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------

const DEFAULT_VALUES = {
  defaultStyle: {
    text: '',
    faceColor: 'white',
    faceRotation: 0,
    fontFamily: 'Arial',
    fontColor: 'black',
    fontStyle: 'normal',
    fontSizeScale: function fontSizeScale(resolution) {
      return resolution / 1.8;
    },
    edgeThickness: 0.1,
    edgeColor: 'black',
    resolution: 200,
  }, // xPlusFaceProperty: null,
  // xMinusFaceProperty: null,
  // yPlusFaceProperty: null,
  // yMinusFaceProperty: null,
  // zPlusFaceProperty: null,
  // zMinusFaceProperty: null,
}; // ----------------------------------------------------------------------------

function extend(publicAPI, model, ...args) {
  const initialValues = arguments.length > 2 && args[2] !== undefined ? args[2] : {};
  Object.assign(model, DEFAULT_VALUES, initialValues); // Inheritance

  vtkActor.extend(publicAPI, model, initialValues);
  macro.get(publicAPI, model, [
    'defaultStyle',
    'xPlusFaceProperty',
    'xMinusFaceProperty',
    'yPlusFaceProperty',
    'yMinusFaceProperty',
    'zPlusFaceProperty',
    'zMinusFaceProperty',
  ]); // Object methods

  vtkAnnotatedCubeActor(publicAPI, model);
} // ----------------------------------------------------------------------------

const newInstance = macro.newInstance(extend, 'vtkAnnotatedCubeActor'); // ----------------------------------------------------------------------------

const vtkAnnotatedCubeActor$1 = {
  newInstance: newInstance,
  extend: extend,
  Presets: AnnotatedCubePresets,
};

export { DEFAULT_VALUES, vtkAnnotatedCubeActor$1 as default, extend, newInstance };
